package com.uraurora.crescent.function.functools.memoizer;

import com.google.common.cache.Cache;
import com.uraurora.crescent.function.Func1;
import com.uraurora.crescent.tuple.ITuple;
import com.uraurora.crescent.tuple.Tuple;

import java.util.concurrent.*;

public class MemoizerFunc1<A, RT> extends AbstractMemoizer<RT> implements Func1<A, RT> {

    private final Func1<? super A, ? extends RT> computable;

    public MemoizerFunc1(Func1<? super A, ? extends RT> computable, Cache<ITuple, Future<RT>> cache){
        super(cache);
        this.computable = computable;
    }

    public MemoizerFunc1(Func1<? super A, ? extends RT> computable){
        super();
        this.computable = computable;
    }

    @Override
    public RT on(A a) {
        return compute(Tuple.of(a), ()->computable.on(a));
    }


    @Deprecated
    @Override
    public MemoizerFunc1<A, RT> memoize(){
        throw new UnsupportedOperationException();
    }

    @Deprecated
    @Override
    public MemoizerFunc1<A, RT> memoize(Cache<ITuple, Future<RT>> cache){
        throw new UnsupportedOperationException();
    }
}
